﻿using System;
using Library.Utils;
using NguyenHoa.Domains;
using NguyenHoa.Services;

namespace NguyenHoa.Web.App_Code
{
  public class GlobalCache
  {
    #region Variables
    private static GlobalCache _instance = null;
    public string DefaultUserPassword = "";
    public string EmailRegularExpressions = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
    public int CachedExpiredDuration = 0;
    public int PageViewDigit = 0;
    public int ItemPerPage = 0;
    public int OtherNewsCount = 0;
    public int MailTimeOut = 0;
    public int ReduceStringLength = 0;
    public int DefaultLanguage = 0;
    public int ExpireBookingCache = 0;
    public int BookingAdultQtt = 0;
    public int BookingChildQtt = 0;
    public int AdvertiseFlashWidth = 0;
    public int AdvertiseFlashHeight = 0;
    public int HomeNewsNumber = 0;
    public bool RewriteOn = false;
    public string Themes = "Aqua";

    public Domains.Language Vietnamese = null;
    public Domains.Language English = null;
    #endregion

    #region Properties
    private TList<Domains.Setting> m_settingList = new TList<Domains.Setting>();

    #region Mail Configure
    private string _SmtpHost = "";
    public string SMTPHost
    {
      get { return _SmtpHost; }
    }

    private string _SmtpUser = "";
    public string SMTPUser
    {
      get { return _SmtpUser; }
    }

    private string _SmtpReceiver = "";
    public string SMTPReceiver
    {
      get { return _SmtpReceiver; }
    }

    private string _SmtpPass = "";
    public string SMTPPass
    {
      get { return _SmtpPass; }
    }

    private bool _SmtpSSL = false;
    public bool SMTPSSL
    {
      get { return _SmtpSSL; }
    }

    private int _SmtpPort = 8080;
    public int SMTPPort
    {
      get { return _SmtpPort; }
    }

    private string _mail_booking;
    public string BookingMail
    {
      get { return _mail_booking; }
    }
    #endregion

    #region Pageview
    private int _pageview = 0;
    public int PageView
    {
      get { return _pageview; }
    }
    #endregion

    #region Reference webs
    private string _link_rateEx = string.Empty;
    public string Link_RateExchange
    {
      get { return _link_rateEx; }
    }

    private string _link_gold = string.Empty;
    public string Link_Gold
    {
      get { return _link_gold; }
    }

    private string _link_stock = string.Empty;
    public string Link_Stock
    {
      get { return _link_stock; }
    }

    private string _link_TV = string.Empty;
    public string Link_TV
    {
      get { return _link_TV; }
    }

    private string _link_loto = string.Empty;
    public string Link_Loto
    {
      get { return _link_loto; }
    }
    #endregion
    #endregion

    #region Methods
    internal static GlobalCache Instance
    {
      get
      {
        if (_instance == null)
        {
          _instance = new GlobalCache();
        }
        return _instance;
      }
    }

    private GlobalCache()
    {
      LoadConfigParameters();
      LoadDBSetting();
      LoadDBValue();
    }

    public void LoadConfigParameters()
    {
      DefaultUserPassword = ConfigurationUtils.ReadParamString("DefaultUserPassword", "123456");
      CachedExpiredDuration = ConfigurationUtils.ReadParamInt32("ExpireCacheMinute", 0);
      PageViewDigit = ConfigurationUtils.ReadParamInt32("PageViewDigit", 6);
      ItemPerPage = ConfigurationUtils.ReadParamInt32("ItemPerPage", 10);
      EmailRegularExpressions = ConfigurationUtils.ReadParamString("EmailRegularExpressions", "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-_]+\\.[a-zA-Z]{2,4}$");
      OtherNewsCount = ConfigurationUtils.ReadParamInt32("OtherNewsCount", 5);
      MailTimeOut = ConfigurationUtils.ReadParamInt32("MailTimeOut", 100000); //default: 1 minute
      ReduceStringLength = ConfigurationUtils.ReadParamInt32("ReduceStringLength", 0);
      DefaultLanguage = ConfigurationUtils.ReadParamInt32("DefaultLanguage", 1);
      BookingAdultQtt = ConfigurationUtils.ReadParamInt32("BookingAdultQtt", 1);
      BookingChildQtt = ConfigurationUtils.ReadParamInt32("BookingChildQtt", 0);
      AdvertiseFlashWidth = ConfigurationUtils.ReadParamInt32("AdvertiseFlashWidth", 226);
      AdvertiseFlashHeight = ConfigurationUtils.ReadParamInt32("AdvertiseFlashHeight", 200);
      HomeNewsNumber = ConfigurationUtils.ReadParamInt32("HomeNewsNumber", 3);
      RewriteOn = ConfigurationUtils.ReadParamBoolean("RewriteOn", false);
      Themes = ConfigurationUtils.ReadParamString("Themes", "Aqua");
    }

    public void LoadDBValue()
    {
      TList<Domains.Language> languageList = new LanguageService().GetAll();
      Vietnamese = languageList.Find("Id", (int)LanguageList.Vietnamese);
      English = languageList.Find("Id", (int)LanguageList.English);
    }

    public void LoadDBSetting()
    {
      m_settingList = new SettingService().GetAll();
      Domains.Setting setting = null;

      #region Mail Configure
      setting = m_settingList.Find("Id", (int)SettingList.SMTP_Host);
      _SmtpHost = (setting != null) ? setting.Value : string.Empty;

      setting = m_settingList.Find("Id", (int)SettingList.SMTP_Pass);
      _SmtpPass = (setting != null) ? setting.Value : string.Empty;

      setting = m_settingList.Find("Id", (int)SettingList.SMTP_Port);
      _SmtpPort = (setting != null) ? Convert.ToInt32(setting.Value) : 8080;

      setting = m_settingList.Find("Id", (int)SettingList.SMTP_SSL);
      _SmtpSSL = (setting != null) ? Convert.ToBoolean(setting.Value) : false;

      setting = m_settingList.Find("Id", (int)SettingList.SMTP_User);
      _SmtpUser = (setting != null) ? setting.Value : string.Empty;

      setting = m_settingList.Find("Id", (int)SettingList.SMTP_Receiver);
      _SmtpReceiver = (setting != null) ? setting.Value : string.Empty;

      setting = m_settingList.Find("Id", (int)SettingList.MAIL_BOOKING);
      _mail_booking = (setting != null) ? setting.Value : string.Empty;
      #endregion

      #region PageView
      setting = m_settingList.Find("Id", (int)SettingList.PAGE_VIEW);
      _pageview = (setting != null) ? Convert.ToInt32(setting.Value) : 0;
      #endregion

      #region Reference Web
      setting = m_settingList.Find("Id", (int)SettingList.RATE_EXCHANGE);
      _link_rateEx = (setting != null) ? setting.Value : string.Empty;

      setting = m_settingList.Find("Id", (int)SettingList.GOLD);
      _link_gold = (setting != null) ? setting.Value : string.Empty;

      setting = m_settingList.Find("Id", (int)SettingList.ONLINE_STOCK);
      _link_stock = (setting != null) ? setting.Value : string.Empty;

      setting = m_settingList.Find("Id", (int)SettingList.TV_ONLINE);
      _link_TV = (setting != null) ? setting.Value : string.Empty;

      setting = m_settingList.Find("Id", (int)SettingList.LOTO);
      _link_loto = (setting != null) ? setting.Value : string.Empty;
      #endregion
    }

    public Setting GetSettingById(int id)
    {
      return m_settingList.Find("Id", id);
    }

    public Setting SetSettingById(int id, string value)
    {
      Setting setting = m_settingList.Find("Id", id);
      setting.Value = value;
      return setting;
    }
    #endregion

    #region Survey
    private Survey m_survey;
    public Survey SurveyQuestion
    {
      set
      {
        m_survey = value;
        m_surveyDetail = null;
      }
    }

    private TList<SurveyDetail> m_surveyDetail;
    public TList<SurveyDetail> SurveyOptions
    {
      get
      {
        if (m_surveyDetail == null && m_survey != null)
        {
          m_surveyDetail = new SurveyDetailService().GetBySurveyId(m_survey.Id);
        }
        return m_surveyDetail;
      }
      set
      {
        m_surveyDetail = null;
      }
    }

    public Survey GetActiveSurvey(int language)
    {
      if (m_survey == null || m_survey.LanguageId != language)
      {
        TList<Domains.Survey> surveyList = new SurveyService().GetActiveByLanguage(language);
        if (surveyList.Count > 0)
        {
          m_survey = surveyList[0];
        }
        else
        {
          m_survey = null;
        }

        m_surveyDetail = null;
      }

      return m_survey;
    }

    #endregion
  }
}